<template>
    <div class="goods-list">
        <!-- <router-link :to="'/home/goodsinfo/'+item.id" class="goods-item" v-for="item of goodsList" :key="item.id">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">¥{{item.sell_price}}</span>
                    <span class="old">¥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>
        </router-link> -->

        <!-- 在网页中，有两种跳转方式 -->
        <!-- 1. 使用a标签的形式叫做标签跳转 -->
        <!-- 2. 使用 window.locatin.href 的形式跳转 -->
        <div @click="getDetail(item.id)" class="goods-item" v-for="item of goodsList" :key="item.id">
            <img :src="item.img_url" alt="">
            <h1 class="title">{{item.title}}</h1>
            <div class="info">
                <p class="price">
                    <span class="now">¥{{item.sell_price}}</span>
                    <span class="old">¥{{item.market_price}}</span>
                </p>
                <p class="sell">
                    <span>热卖中</span>
                    <span>剩{{item.stock_quantity}}件</span>
                </p>
            </div>
        </div>
        <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
    </div>
</template>

<script>
export default {
  data() {
    return {
      pageIndex: 1,
      goodsList: []
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getGoodsList() {
      this.$http
        .get("getGoodsList?pageIndex=" + this.pageIndex)
        .then(result => {
          const datas = result.body.map(item => {
            item.id = this.goodsList.length + item.id;
            return item;
          });
          this.goodsList = this.goodsList.concat(datas);
        })
        .catch(err => {
          console.log("加载失败");
        });
    },
    getMore() {
      if (this.pageIndex >= 10) {
        this.pageIndex = 1;
        this.goodsList = [];
      }
      this.pageIndex++;
      this.getGoodsList();
    },
    getDetail(id){
        //使用JS的形式进行路由跳转
        // this.$router.push({"path":'/home/goodsinfo/'+id})
        this.$router.push({name:'goodsInfo',params:{id}})
        // this.$router.push({path:'/home/goodsinfo',query:{id}})
        // this.$router.push('/home/goodsinfo/'+id)
    }
  }
};
</script>
<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;
  .goods-item {
    width: 49%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 3px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 293px;
    img {
      width: 100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: red;
          font-size: 16px;
          font-weight: bold;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
    }
    .sell {
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>

